<?php

namespace common\model;


use think\model\RelationModel;
use think\module\Module;

class ReviewFeedbackModel extends RelationModel
{
    const STATUS_NOT_STARTED = 'not_started';
    const STATUS_IN_PROGRESS = 'in_progress';
    const STATUS_FINAL = 'final';

    const PERMISSIONS_ON = 'on';
    const PERMISSIONS_OFF = 'off';
    const PERMISSIONS_ONE = 'one';
    const PERMISSIONS_TWO = 'two';

    const DEMAND_TYPE_REVIEW_FEEDBACK = 'review_feedback'; // 审核反馈
    const DEMAND_TYPE_DEMAND_ORDER = 'demand_order';  // 需求单

    const STANDARD_APPEND_YES = 'yes'; // 标准新增
    const STANDARD_APPEND_NO = 'no'; // 非标准新增


    //自动验证
    protected $_validate = [
        ['title', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['project_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['json', '', '', self::EXISTS_VALIDATE, 'array'],
        ['module_code', ['asset', 'shot'], '', self::EXISTS_VALIDATE, 'in'],
        ['demand_type', ['demand_order', 'review_feedback'], '', self::EXISTS_VALIDATE, 'in'],
        ['demand_order_id', '', '', self::EXISTS_VALIDATE, 'integer'],
    ];

    //自动完成
    protected $_auto = [
        ['json', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
    ];

    // 隐藏字段
    protected $hidden = ['json'];

    /**
     * 获取器：自定义字段
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getJsonAttr($value, $data)
    {
        // 当前模块的自定义字段配置
        $currentModuleCode = \request()->getModuleCode();
        $fieldDict = Module::$moduleDictData['field_index_by_code']['review_feedback']['custom'];

        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        // 把自定义值写入追加字段
        foreach ($fieldDict as $filed => $config) {
            if (array_key_exists($filed, $jsonDictData)) {
                $this->appendCustomField[$filed] = $jsonDictData[$filed];
            } else {
                // 没有值赋空值
                $this->appendCustomField[$filed] = "";
            }
        }

        return $jsonDictData;
    }
}
